/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc.example.persistence.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mvc.example.bussines.domain.Student;
import mvc.example.bussines.domain.StudentStatus;
import mvc.example.persistence.dao.StudentDAO;
import mvc.example.persistence.dao.exception.DAOException;

/**
 *
 * @author Zeta
 */
public class StudentDAOImplJDBC extends DAOImplJDBC implements StudentDAO{

    @Override
    public void save(Student entity) throws DAOException {
        String sql = "INSERT INTO student (lastname, firstname, dni, studentId, status) VALUES (?,?,?,?,?)";
        try {
            PreparedStatement prepareStatement = getConnection().prepareStatement(sql);
            prepareStatement.setString(1, entity.getLastname());
            prepareStatement.setString(2, entity.getFirstname());
            prepareStatement.setString(3, entity.getDni());
            prepareStatement.setString(4, entity.getStudentId());
            prepareStatement.setString(5, entity.getStatus().toString());
            
            prepareStatement.executeUpdate();
            ResultSet result = prepareStatement.executeQuery("SELECT LAST_INSERT_ID()");
            if (result.first()) {
                entity.setId(result.getInt(1));
            }
        } catch (SQLException ex) {
            throw new DAOException(ex.getMessage());
        }
    }

    @Override
    public void update(Student entity) throws DAOException {
         String sql = "UPDATE student SET lastname = ? , firstname = ? , dni = ? , studentId = ? , status = ? WHERE id=?";
        try {
            PreparedStatement prepareStatement = getConnection().prepareStatement(sql);
            prepareStatement.setString(1, entity.getLastname());
            prepareStatement.setString(2, entity.getFirstname());
            prepareStatement.setString(3, entity.getDni());
            prepareStatement.setString(4, entity.getStudentId());
            prepareStatement.setString(5, entity.getStatus().toString());
            prepareStatement.setInt(6, entity.getId());
            prepareStatement.executeUpdate();
        } catch (SQLException ex) {
            throw new DAOException(ex.getMessage());
        }
    }

    @Override
    public Student find(Integer id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Integer id) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public List<Student> getAll() {
       
        List<Student> students = new ArrayList<Student>();
        
        String sql = "SELECT id, lastname, firstname, dni, studentId, status FROM student";
        try {
            PreparedStatement prepareStatement = getConnection().prepareStatement(sql);
            ResultSet resultSet = prepareStatement.executeQuery();
            while (resultSet.next()) {
                students.add(parseResulsetToStudent(resultSet));
            }
        } catch (SQLException ex) {
            Logger.getLogger(StudentDAOImplJDBC.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return students;
    }

    @Override
    public List<Student> getStudentsByStatus(StudentStatus status) {
       List<Student> students = new ArrayList<Student>();
        
        String sql = "SELECT id, lastname, firstname, dni, studentId, status FROM student WHERE status = ?";
        try {
            PreparedStatement prepareStatement = getConnection().prepareStatement(sql);
            prepareStatement.setString(1, status.toString());
            ResultSet resultSet = prepareStatement.executeQuery();
            while (resultSet.next()) {
                students.add(parseResulsetToStudent(resultSet));
            }
        } catch (SQLException ex) {
            Logger.getLogger(StudentDAOImplJDBC.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return students;
    }

    @Override
    public Student getByStudentId(String studentId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private Student parseResulsetToStudent(ResultSet resultSet) throws SQLException {
        Student student = new Student();
        student.setId(resultSet.getInt("id"));
        student.setLastname(resultSet.getString("lastname"));
        student.setFirstname(resultSet.getString("firstname"));
        student.setDni(resultSet.getString("dni"));
        student.setStudentId(resultSet.getString("studentId"));
        student.setStatus(StudentStatus.valueOf(resultSet.getString("status")));
        return student;
    }
    
    
}
